<?php

namespace app\admin\controller\business;

// 引入基类控制器
use app\common\controller\Backend;

class Privateseainfo extends Backend
{
    // 关联查询
    protected $relationSearch = true;

    protected $BusinessModel;
    protected $ReceiveModel;

    public function __construct()
    {
        parent::__construct();

        // 加载模型
        $this->model = model('Business.Visit');
        $this->BusinessModel = model('Business.Business');
        $this->ReceiveModel = model('Business.Receive');
    }

    public function index($ids = null)
    {
        $busDate = $this->BusinessModel
            // ->with(['source', 'admin' => ['id', 'username']])
            ->where('id', $ids)
            ->find();


        if (!$busDate) {
            $this->error('客户不存在');
            exit;
        }

        // myPrinf($busDate->toArray());
        // exit;

        $this->view->assign('busDate', $busDate);

        return $this->view->fetch();
    }
    // 申请记录
    public function receive($ids = null)
    {
        if (empty($ids)) {
            $this->error(__('No Results were found'));
            exit;
        }

        $temp = $this->model;
        $this->model = null;

        list($where, $sort, $order, $offset, $limit) = $this->buildparams('', true);

        $this->model = $temp;

        if ($this->request->isAjax()) {
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }



            //设置过滤方法
            $this->request->filter(['strip_tags']);

            $total = $this->ReceiveModel
                ->where($where)
                ->where('busid', $ids)
                // ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();



            $list = $this->ReceiveModel
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();

            $result = [
                'rows' => $list,
                'total' => $total
            ];

            return json($result);
        }
    }

    // 回访记录
    public function visit($ids = null)
    {
        //设置过滤方法
        $this->request->filter(['strip_tags']);

        if ($this->request->isAjax()) {

            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->count();  //查询总数

            $list = $this->model
                ->with(['admin', 'business'])
                ->where($where)
                ->where('busid', $ids)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();  //查询数据

            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
    }
    // 添加回访记录
    public function add($ids = null)
    {
        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');

            $data = [
                'content' => $params['content'],
                'busid' => $ids,
                'adminid' => $this->auth->id
            ];

            if (empty($data['content'])) {
                $this->error('内容不能为空');
            }


            $result = $this->model->validate('common/Business/Visit')->save($data);

            if ($result) {
                $this->success();
            } else {
                $this->error($this->model->getError());
            }

            exit;
        }
        return  $this->view->fetch();
    }
    // 编辑回访记录
    public function edit($ids = null)
    {
        $row = $this->model->find($ids);

        if (!$row) {
            $this->error(__('未找到当前回访记录'));
            exit;
        }

        if ($this->request->isPost()) {
            $params = $this->request->post('row/a');

            $data = [
                'id' => $ids,
                'content' => $params['content'],
                'busid' => $row['busid'],
                'adminid' => $row['adminid'],
            ];

            if (empty($data['content'])) {
                $this->error('内容不能为空');
            }


            $result = $this->model->validate('common/Business/Visit')->isUpdate(true)->save($data);

            if ($result) {
                $this->success();
            } else {
                $this->error($this->model->getError());
            }

            exit;
        }

        $this->view->assign('row', $row);

        return $this->view->fetch();
    }
    // 删除回访记录
    public function del($ids = null)
    {
        $ids = !empty($ids) ?  explode(',', $ids) : [];

        $row = $this->model->column('id');

        foreach ($ids as $item) {
            if (!in_array($item, $row)) {
                $this->error(__('没有找到该回访记录'));
                exit;
            }
        }

        $result = $this->model->whereIn('id', $ids)->delete();

        if ($result) {
            $this->success();
        } else {
            $this->error($this->model->getError());
        }

        exit;
    }
}
